package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.SysDictTypeMapper;
import com.itheima.pojo.SysDictType;
import com.itheima.service.SysDictTypeService;
import com.itheima.utils.RequestUtils;
import com.itheima.utils.UuidUtil;
import com.itheima.vo.SysDictTypeVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Service
public class SysDictTypeServiceImpl implements SysDictTypeService {
    @Autowired
    private SysDictTypeMapper sysDictTypeMapper;

    /**
     * 添加字典类型
     * @param sysDictTypeVo
     */
    @Override
    @Transactional
    public void add(SysDictTypeVo sysDictTypeVo) {
        SysDictType sysDictType = new SysDictType();
        BeanUtils.copyProperties(sysDictTypeVo,sysDictType);
        sysDictType.setUid(UuidUtil.getUuid());
        sysDictType.setCreateTime(new Date());
        sysDictType.setUpdateTime(new Date());
        //TODO 创建人uid
        sysDictType.setCreateByUid("1f01cd1d2f474743b241d74008b12333");
        //TODO 更新人uid
        sysDictType.setUpdateByUid("1f01cd1d2f474743b241d74008b12333");
        sysDictTypeMapper.insertSelective(sysDictType);
    }

    /**
     * 批量删除字典类型
     * @param sysDictTypeVoList
     */
    @Override
    @Transactional
    public void deleteBatch(List<SysDictTypeVo> sysDictTypeVoList) {
         if (sysDictTypeVoList==null){
            throw new RuntimeException("字典类型不存在");
         }
        for (SysDictTypeVo sysDictTypeVo : sysDictTypeVoList) {
            SysDictType sysDictType = new SysDictType();
            BeanUtils.copyProperties(sysDictTypeVo,sysDictType);
            int i = sysDictTypeMapper.deleteByPrimaryKey(sysDictType);
            if (i<=0){
                throw new RuntimeException("字典类型不存在");
            }
        }


    }

    /**
     * 条件查询获取字典类型列表
     * @param sysDictTypeVo
     * @return
     */
    @Override
    public PageInfo list(SysDictTypeVo sysDictTypeVo) {
        if (sysDictTypeVo==null){
            return new PageInfo(sysDictTypeMapper.selectAll());
        }
        if (sysDictTypeVo.getCurrentPage()!=null&&sysDictTypeVo.getPageSize()!=null){
            PageHelper.startPage(sysDictTypeVo.getCurrentPage(),sysDictTypeVo.getPageSize());
        }
        Example example = new Example(SysDictType.class);
        Example.Criteria criteria = example.createCriteria();
        if (sysDictTypeVo.getDictName()!=null&&!"".equals(sysDictTypeVo.getDictName())){
            criteria.orLike("dictName","%"+sysDictTypeVo.getDictName()+"%");
        }
        if (sysDictTypeVo.getDictType()!=null&&!"".equals(sysDictTypeVo.getDictType())){
            criteria.orLike("dictType","%"+sysDictTypeVo.getDictType()+"%");
        }
        List<SysDictType> sysDictTypeList = sysDictTypeMapper.selectByExample(example);
        return new PageInfo(sysDictTypeList);
    }

    @Override
    @Transactional
    public void edit(SysDictTypeVo sysDictTypeVo) {
        SysDictType sysDictType = new SysDictType();
        BeanUtils.copyProperties(sysDictTypeVo,sysDictType);
        sysDictType.setUpdateTime(new Date());
        //修改更新人Uid
        sysDictType.setUpdateByUid("1723ad77a1ca06b29a011471036a2dd6");
        sysDictTypeMapper.updateByPrimaryKeySelective(sysDictType);
    }
}
